/*
 * Copyright 2015-2016 Tumblr, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tumblr.backboard.example;

import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringSystem;
import com.tumblr.backboard.Utils.View;
import com.tumblr.backboard.imitator.ToggleImitator;
import com.tumblr.backboard.performer.MapPerformer;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.element.ShapeElement;

import java.util.ArrayList;
import java.util.List;

/**
 * A ring of views that bloom and then contract.
 *
 * @author ericleong
 * @since 2021-05-07
 */
public class BloomFragment extends BaseFraction {
    private static final float DIAMETER = 80.0f;

    private static final float RING_DIAMETER = 5 * DIAMETER;

    private static final int CIRCLES_SIZE = 6;

    private static final int DOUBLE = 2;

    private static final int OPEN = 1;

    private static final int CLOSED = 0;

    private Component[] mCircles;
    private DependentLayout mRootView;

    @Override
    public int getUiContent() {
        return ResourceTable.Layout_fragment_bloom;
    }

    @Override
    public void initComponent() {
        mRootView = (DependentLayout) mComponent;
        mCircles = new Component[CIRCLES_SIZE];

        final List<Integer> circles = new ArrayList<>();
        circles.add(ResourceTable.Graphic_circle_green);
        circles.add(ResourceTable.Graphic_circle_red);
        circles.add(ResourceTable.Graphic_circle_purple);
        circles.add(ResourceTable.Graphic_circle_orange);
        circles.add(ResourceTable.Graphic_circle_blue);
        circles.add(ResourceTable.Graphic_circle_gray);

        // layout params
        DependentLayout.LayoutConfig params = new DependentLayout.LayoutConfig(AttrHelper.vp2px(DIAMETER,
                getFractionAbility()),
                AttrHelper.vp2px(DIAMETER, getFractionAbility()));
        params.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
        int colorIndex = 0;
        for (int index = 0; index < mCircles.length; index++) {
            mCircles[index] = new Component(getFractionAbility());

            mCircles[index].setLayoutConfig(params);
            ShapeElement shapeElement = new ShapeElement(getFractionAbility(), circles.get(colorIndex));

            mCircles[index].setBackground(shapeElement);

            colorIndex++;
            if (colorIndex >= circles.size()) {
                colorIndex = 0;
            }
            mRootView.addComponent(mCircles[index]);
        }

        circles.clear();

        final SpringSystem springSystem = SpringSystem.create();

        final Spring spring = springSystem.createSpring();

        double arc = DOUBLE * Math.PI / mCircles.length;

        for (int index = 0; index < mCircles.length; index++) {
            Component view = mCircles[index];

            spring.addListener(new MapPerformer(view, View.TRANSLATION_X, 0, 1,
                    0, (float) (RING_DIAMETER * Math.cos(index * arc))));

            spring.addListener(new MapPerformer(view, View.TRANSLATION_Y, 0, 1,
                    0, (float) (RING_DIAMETER * Math.sin(index * arc))));

            spring.setEndValue(CLOSED);
        }
        mRootView.setTouchEventListener(new ToggleImitator(spring, CLOSED, OPEN));
    }
}
